package root.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import root.service.ArticleService;
import root.utils.StringHelper;

import java.util.List;

/**
 * @author 吕梁山
 * @date 2019-01-19
 */
public class ArticleAction extends BaseAction {

    private ArticleService articleService = new ArticleService();

    public void getArticlePage() {
        JSONObject jsonObject = new JSONObject();
        String article_type = getPara("article_type");
        String article_title = getPara("article_title");
        String status = getPara("status");
        int pageNumber = getParaToInt("pageNumber", 1);
        Page<Record> articlePage = articleService.getArticlePage(article_title, article_type, status, pageNumber, 15);
        jsonObject.put("code", 0);
        jsonObject.put("page", articlePage);
        renderJson(jsonObject);
    }

    public void getArticle() {
        JSONObject jsonObject = new JSONObject();
        String articleId = getPara("articleId");
        Record article = articleService.getArticle(articleId);
        jsonObject.put("code", 0);
        jsonObject.put("article", article);
        renderJson(jsonObject);
    }

    public void saveArticle() {
        JSONObject jsonObject = new JSONObject();
        String articleId = getPara("articleId");
        String article_title = getPara("article_title");
        String article_content = getPara("article_content");
        String article_type = getPara("article_type");
        Record article = new Record();
        if(StringHelper.isNotEmpty(articleId)){
            article.set("id", articleId);
        }
        article.set("article_title", article_title);
        article.set("article_content", article_content);
        article.set("article_type", article_type);
        boolean b = articleService.saveArticle(article);
        renderJson(returnByBool(b));
    }

    public void getArticleTypePage(){
        JSONObject jsonObject = new JSONObject();
        int pageNumber = getParaToInt("pageNumber");
        Page<Record> articlePage = articleService.getArticleTypePage(pageNumber, 15);
        jsonObject.put("code", 0);
        jsonObject.put("page", articlePage);
        renderJson(jsonObject);
    }

    public void getAllArticleType(){
        JSONObject jsonObject = new JSONObject();
        List<Record> allArticleType = articleService.getAllArticleType();
        jsonObject.put("code", 0);
        jsonObject.put("articeList", allArticleType);
        renderJson(jsonObject);
    }

    public void getArticleType(){
        JSONObject jsonObject = new JSONObject();
        String id = getPara("id");
        Record record = articleService.getArticleType(id);
        jsonObject.put("code", 0);
        jsonObject.put("articleType", record);
        renderJson(jsonObject);
    }

    public void saveArticleType(){
        String id = getPara("id");
        String type_name = getPara("type_name");
        Record record = new Record();
        if(StringHelper.isNotEmpty(id)){
            record.set("id", id);
        }
        record.set("type_name", type_name);
        boolean b = articleService.saveArticleType(record);
        renderJson(returnByBool(b));
    }
}
